<?php

namespace app\api\controller\store;

use app\api\controller\BaseController;
use app\models\system\SystemRegion;
use app\models\system\SystemStore;
use services\GroupDataService;
use services\SystemConfigService;
use app\Request;
use services\UtilService;
use think\facade\Db;
use utils\Json;

/**
 * 个人中心的部分接口
 * Class IndexController
 * @package app\api\controller\store
 */
class IndexController extends BaseController
{
    /**
     * 关于我们
     * @param Request $request
     * @return Json
     */
    public function aboutUs(Request $request)
    {
        // 获取设置中的值
        $config = SystemConfigService::more([
            "routine_logo",
            "wechat_share_title",
            "business",
            "site_phone"
        ]);
        return app("json")->successful($config);
    }

    /**
     * 帮助反馈
     * @param Request $request
     * @return Json
     */
    public function helpQuestion(Request $request)
    {
        $list = GroupDataService::getData("help_question");
        return app("json")->successful($list);
    }

    /**
     * @param Request $request
     * @param $city_name string
     * @return Json
     */
    public function storeList(Request $request, $city_name = "")
    {
        if (!$city_name) {
            $list = SystemStore::where("wxapp_id", $this->wxapp_id)
                ->where("is_show", 1)->where("is_del", 0)
                ->field("id, name, introduction, phone, address, detailed_address, image, latitude, longitude, day_time")
                ->order("id DESC")
                ->select()->toArray();
        } else {
            $city_name = str_replace("市", "", $city_name);
            $list = SystemStore::where("wxapp_id", $this->wxapp_id)
                ->where("is_show", 1)
                ->where("address", "like", "%{$city_name}%")
                ->field("id, name, introduction, phone, address, detailed_address, image, latitude, longitude, day_time")
                ->order("id DESC")
                ->select()
                ->toArray();
        }
        return app("json")->successful($list);
    }


    /**
     * 获取列表下面的城市
     * @param Request $request
     * @param int $city_id
     * @return Json
     */
    public function listStore(Request $request, $city_id = 0)
    {
        // 获取该城市下面的商城
        $count = SystemStore::where("wxapp_id", $this->wxapp_id)->where("is_show", 1)->where("is_del", 0)->where("city_id", $city_id)->count();
        if (!$count) {
            $list = SystemStore::where("wxapp_id", $this->wxapp_id)
                ->where("is_show", 1)
                ->where("is_del", 0)
                ->order("id DESC")
                ->select()->each(function ($item, $key){
                    // 获取门店的机器的数量
                    $device_list = Db::name("store_device")
                        ->where('store_id', $item['id'])
                        ->field('id, device_name, device_no, car_order_id')
                        ->where('is_del', 0)
                        ->select()->each(function ($item, $key){
                            if ($item['car_order_id']) {
                                $item['is_free'] = false;
                            } else {
                                $item['is_free'] = true;
                            }
                            return $item;
                        });
                    $item['device_list'] = $device_list;
                    $count = count($device_list);
                    $flag = Db::name("store_device")
                        ->where('store_id', $item['id'])
                        ->where('is_del', 0)
                        ->where('car_order_id', 0)
                        ->count();
                    if ($item['is_work'] == 0) {
                        $item['free'] = 3;
                    } elseif ($count == $flag) {
                        $item['free'] = 1;
                    } else {
                        $item['free'] = 2;
                    }
                    return $item;
                });
        } else {
            $list = SystemStore::where("wxapp_id", $this->wxapp_id)
                ->where("is_show", 1)
                ->where("is_del", 0)
                ->where("city_id", $city_id)
                ->order("id DESC")
                ->select()->each(function ($item, $key){
                    // 获取门店的机器的数量
                    $device_list = Db::name("store_device")
                        ->where('store_id', $item['id'])
                        ->field('id, device_name, device_no, car_order_id')
                        ->where('is_del', 0)
                        ->select()->each(function ($item, $key){
                            if ($item['car_order_id']) {
                                $item['is_free'] = false;
                            } else {
                                $item['is_free'] = true;
                            }
                            return $item;
                        });
                    $item['device_list'] = $device_list;
                    $count = count($device_list);
                    $flag = Db::name("store_device")
                        ->where('store_id', $item['id'])
                        ->where('is_del', 0)
                        ->where('car_order_id', 0)
                        ->count();
                    if ($item['is_work'] == 0) {
                        $item['free'] = 3;
                    } elseif ($count == $flag) {
                        $item['free'] = 1;
                    } else {
                        $item['free'] = 2;
                    }
                    return $item;
                });
        }
        return app("json")->successful(compact('list'));

    }

    /**
     * 获取所有城市
     * @param Request $request
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function cityList(Request $request)
    {
        $cityIdList = SystemStore::where("wxapp_id", $this->wxapp_id)->where("is_show", 1)->where("is_del", 0)->column("city_id");
        $cityList = SystemRegion::field("id, name, merger_name")->order("id DESC")->where("id", "in", $cityIdList)->select()->toArray();
        return app("json")->success(compact("cityList"));
    }
}